package jnpf.database.util;

import com.alibaba.druid.pool.DruidDataSource;
import jnpf.database.enums.DbSttEnum;
import jnpf.exception.DataException;

import java.util.Random;


/**
 * 数据库字段数据装配工具类
 *
 * @author JNPF开发平台组
 * @version V3.2.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/6/21
 */
public class CommonUtil {

    public static String getReplaceSql(String sql, String table, DataSourceUtil dbSourceOrDbLink,
                                       Boolean dbNameFlag, Boolean schemaFlag, Boolean tableSpaceFlag) {
        if (table != null && !"".equals(table)) {
            sql = sql.replace(DbSttEnum.TABLE.getTarget(), table);
        }
        if (dbNameFlag) {
            String dbName = dbSourceOrDbLink.getDbName();
            sql = sql.replace(DbSttEnum.DB_NAME.getTarget(), dbName);
        }
        if (schemaFlag) {
            String schema = dbSourceOrDbLink.getUserName();
            sql = sql.replace(DbSttEnum.DB_SCHEMA.getTarget(), schema);
        }
        if (tableSpaceFlag) {
            String tableSpace = dbSourceOrDbLink.getTableSpace();
            sql = sql.replace(DbSttEnum.TABLE_SPACE.getTarget(), tableSpace);
        }
        return sql;
    }

    /**
     * 获取DruidDataSource
     *
     * @param dataSourceUtil
     * @return
     */
    public static DruidDataSource getDruidDataSource(DataSourceUtil dataSourceUtil) throws DataException {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUsername(dataSourceUtil.getUserName());
        druidDataSource.setPassword(dataSourceUtil.getPassword());
        //jdbc-url
        druidDataSource.setUrl(DbTypeUtil.getUrl(dataSourceUtil));
        //数据库驱动
        druidDataSource.setDriverClassName(DbTypeUtil.getDb(dataSourceUtil).getDriver());
        return druidDataSource;
    }

    /**
     * 随机生成包含大小写字母及数字的字符串
     *
     * @param length
     * @return
     */
    public static String getStringRandom(int length) {
        String val = "";
        Random random = new Random();
        //参数length，表示生成几位随机数
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if ("char".equalsIgnoreCase(charOrNum)) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (random.nextInt(26) + temp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

}

